$(function() {
	preRun();
	var IE='\v'=='v';
	if(IE) {
		run();
	} else {
		window.onload = run;
	}
});

function preRun() {
	if ($('#country-menu').length > 0) {
		$('.countries-list .loading').show();
	}
}

function run() {
	var links = [];
	if ($('#country-menu').length > 0) {
		$('#country-menu a').each(function() {
			links.push($(this).parents(0).html());
		});
	}

	var colsCount = 4;
	var blocksize = Math.floor(links.length/colsCount);
	var blocksizeRest = links.length - colsCount * blocksize;
	var iter = 0;
	for (var j = 0; j < colsCount; j++) {
		var ul = $('<ul class="menu countries"></ul>');
		var blockSizeIter = blocksize;
		if(blocksizeRest > 0) {
			--blocksizeRest;
			blockSizeIter++;
		}
		for (var k = 0; k < blockSizeIter; k++) {
			var cItem = links[iter++];
			if (cItem == undefined) {
				break;
			}
			ul.append('<li>' + cItem + '</li>');
		}
		$('.countries-list').append(ul);
		$('.loading').hide();
	}
	

	$('a.close-block').click(function() {
		$(this).parents('.sidebar-right').hide();
		$('.content-cols .content-outer .content').css('marginRight', '0');
		$('a.other-countries').show();
	});

	$('a.other-countries').click(function() {
		if ($('.sidebar-right').is(':hidden')) {
			$('.sidebar-right').show();
			$('.content-cols .content-outer .content').css('marginRight', $('.sidebar-right').width() + 9 + 'px');
			$(this).hide();
		}
	});

}